package com.crawler.util.contex.impl;

import com.crawler.util.contex.ContentExtractor;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * @author guorui1
 */
public abstract class HtmlBaseContentExtractor implements ContentExtractor {

    private String expression;

    public HtmlBaseContentExtractor(String expression) {
        this.expression = expression;
    }

    @Override
    public String extract(String html) {
        Document dirtyDocument = Jsoup.parse(html);
        Element element = TagBaseCleaner.clean(dirtyDocument).body();
        Elements elements = element.select(selector());
        return elements == null || elements.isEmpty() ? null : elements.text();
    }

    /**
     * 选择器
     *
     * @return 返回常见选择器表达式
     */
    public abstract String selector();

    public String getExpression() {
        return expression;
    }

    public void setExpression(String expression) {
        this.expression = expression;
    }
}
